﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Architecture.ComponentLogic.ElementModel;

namespace AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.Process
{
    public class ElementBuilderProcessEntity : ElementBuilder
    {
        public override void BuildStructure(AddIn.EA.Architecture.ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            BuildProcessEntityElement(viewModel, templateRepository, itemEntity);
            BuildProcessEntityFilterElement(viewModel, templateRepository, itemEntity);
            BuildProcessEntityFilterKeyElement(viewModel, templateRepository, itemEntity);
            BuildProcessEntityCatalogElement(viewModel, templateRepository, itemEntity);
        }
        public void BuildProcessEntityElement(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateProcessEntityElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the ProcessEntity package
            ComponentLogic.ElementModel.Package processEntityPackage = GetProcessEntityPackage(viewModel);

            //Set ProcessEntity name.
            String elementName = templateProcessEntityElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            processEntityPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Create the Entity Element
            Element ProcessEntityElement = BuildEntityElementFromTemplate(itemEntity, templateProcessEntityElement, elementName);

            BindElementProperties(ProcessEntityElement, itemEntity);

            processEntityPackage.Elements.Add(ProcessEntityElement);
        }
        public void BuildProcessEntityFilterElement(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateProcessEntityParameterFilterElement = templatePackage.GetPackageByName("Parameter").GetElementByName("Filtro[entity_name]");

            ComponentLogic.ElementModel.Package processEntityPackage = GetProcessEntityPackage(viewModel);
            ComponentLogic.ElementModel.Package parameterPackage = GetViewModelPackage("Parameter", processEntityPackage, true);

            String elementFilterName = templateProcessEntityParameterFilterElement.Name.Replace("[entity_name]", itemEntity.Name);
            //Prevent existing item before include.
            parameterPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementFilterName) == 0);

            //Build the filter element.
            Element ProcessEntityFilterElement = BuildEntityElementFromTemplate(itemEntity, templateProcessEntityParameterFilterElement, elementFilterName);

            foreach (ComponentLogic.StandardProject.PropertyStandardProject itemProperty in itemEntity.Properties.FindAll(it => it.ComposeFilter))
                ProcessEntityFilterElement.Attributes.Add(CreateElementModelAttribute(itemProperty));

            parameterPackage.Elements.Add(ProcessEntityFilterElement);
        }
        public void BuildProcessEntityFilterKeyElement(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateProcessEntityParameterKeyElement = templatePackage.GetPackageByName("Parameter").GetElementByName("Filtro[entity_name]Chave");

            ComponentLogic.ElementModel.Package ProcessEntityPackage = GetProcessEntityPackage(viewModel);
            ComponentLogic.ElementModel.Package parameterPackage = GetViewModelPackage("Parameter", ProcessEntityPackage, true);

            String elementKeyName = templateProcessEntityParameterKeyElement.Name.Replace("[entity_name]", itemEntity.Name);
            //Prevent existing item before include.
            parameterPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementKeyName) == 0);

            //Build the filter element.
            Element ProcessEntityKeyElement = BuildEntityElementFromTemplate(itemEntity, templateProcessEntityParameterKeyElement, elementKeyName);

            foreach (ComponentLogic.StandardProject.PropertyStandardProject itemProperty in itemEntity.Properties.FindAll(it => it.ComposeKey))
                ProcessEntityKeyElement.Attributes.Add(CreateElementModelAttribute(itemProperty));

            parameterPackage.Elements.Add(ProcessEntityKeyElement);
        }
        public void BuildProcessEntityCatalogElement(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateProcessEntityCatalogElement = templatePackage.GetPackageByName("Catalog").GetElementByName("[entity_name]");

            ComponentLogic.ElementModel.Package ProcessEntityPackage = GetProcessEntityPackage(viewModel);
            ComponentLogic.ElementModel.Package catalogPackage = GetViewModelPackage("Catalog", ProcessEntityPackage, true);

            String elementName = templateProcessEntityCatalogElement.Name.Replace("[entity_name]", itemEntity.Name);
            //Prevent existing item before include.
            catalogPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Build the filter element.
            Element processEntityCatalogElement = BuildEntityElementFromTemplate(itemEntity, templateProcessEntityCatalogElement, elementName);

            catalogPackage.Elements.Add(processEntityCatalogElement);
        }
        private Framework.Package GetTemplatePackage(Framework.RepositoryManager templateRepository)
        {
            return templateRepository.GetPackageByName("Process").GetPackageByName("ProcessEntity");
        }
        private Package GetProcessEntityPackage(ComponentLogic.ElementModel.Package viewModel)
        {
            ComponentLogic.ElementModel.Package processPackage = GetViewModelPackage("Process", viewModel, true);
            ComponentLogic.ElementModel.Package processEntityPackage = GetViewModelPackage("ProcessEntity", processPackage, true);
            return processEntityPackage;
        }
        public override void Validate()
        {

        }
    }
}
